/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package name.coolwind.cxblog.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import name.coolwind.cxblog.domain.Blog;
import name.coolwind.cxblog.domain.Tag;
import name.coolwind.cxblog.service.BlogService;
import name.coolwind.cxblog.service.TagService;
import name.coolwind.cxblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author coolwind
 */
@Controller
@RequestMapping(value = "/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;
    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String listBlogPage(ModelMap model) {
        //this.blogService.setPerPage(2);
        List<Blog> blogList = this.blogService.getBlogsByPage(0, /*this.blogService.getPerPage()*/ 20);
        List<String> userNameList = new ArrayList<String>();
        List<String> tagNameList = new ArrayList<String>();
        List<Tag> tagList = this.tagService.getHotTag();
        List<String> archList = this.blogService.getRencentArc();
        int total = this.blogService.getAmountOfBlog();
        
        
        for (Blog tmp : blogList) {
            String tmpUserName = this.userService.
                    getUserNameByUserId(String.valueOf(tmp.getBlogAuthor()));
            String tmpTagName = this.tagService.
                    getTagByTagId(String.valueOf(tmp.getBlogTag()));
            userNameList.add(tmpUserName);
            tagNameList.add(tmpTagName);
        }

        model.put("blogList", blogList);
        model.put("userNameList", userNameList);
        model.put("tagNameList", tagNameList);
        model.put("tagList", tagList);
        model.put("archList", archList);
        model.put("page", 0);
        model.put("total", total);
        
        return "index";
    }

    @RequestMapping(value = "/page/{page_id}", method = RequestMethod.GET)
    public String listBlogPageByPageIndex(@PathVariable String page_id, ModelMap model) {
        List<Blog> blogList = this.blogService.getBlogsByPage(Integer.valueOf(page_id) * 20, 20/*this.blogService.getPerPage(), this.blogService.getPerPage()*/);
        List<String> userNameList = new ArrayList<String>();
        List<String> tagNameList = new ArrayList<String>();
        List<Tag> tagList = this.tagService.getHotTag();
        List<String> archList = this.blogService.getRencentArc();
        int total = this.blogService.getAmountOfBlog();
        
        for (Blog tmp : blogList) {
            String tmpUserName = this.userService.
                    getUserNameByUserId(String.valueOf(tmp.getBlogAuthor()));
            String tmpTagName = this.tagService.
                    getTagByTagId(String.valueOf(tmp.getBlogTag()));
            userNameList.add(tmpUserName);
            tagNameList.add(tmpTagName);
        }

        model.put("blogList", blogList);
        model.put("userNameList", userNameList);
        model.put("tagNameList", tagNameList);
        model.put("tagList", tagList);
        model.put("archList", archList);
        model.put("page", page_id);
        model.put("total", total);
        
        return "index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addBlogPage(@CookieValue(value = "user_id", required = false) String userId,
            BlogCommand blogCommand, ModelMap model) {
        if (userId == null) {
            return "error/permission";
        }
        List<Tag> tagList = this.tagService.getAllTags();
        model.put("tagList", tagList);
        return "blog/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addBlogAction(@CookieValue(value = "user_id", required = false) String userId, BlogCommand blogCommand) {
        Blog blog = new Blog();
        blog.setBlogAuthor(Integer.valueOf(userId));
        blog.setBlogClick(0);
        blog.setBlogContent(blogCommand.getBlogContent());
        blog.setBlogTag(blogCommand.getBlogTag());
        blog.setBlogTitle(blogCommand.getBlogTitle());
        blog.setBlogDate(new Date());

        blogService.createBlog(blog);
        return "redirect:/blog";
    }

    @RequestMapping(value = "/{blogid}/delete", method = RequestMethod.POST)
    public String deleteBlogPage(@PathVariable String blogid,
            HttpServletRequest request) {
        return "blof/delete";
    }

    @RequestMapping(value = "/{blogid}/update", method = RequestMethod.GET)
    public String editBlogPage(@PathVariable String blogid) {
        return "blog/update";
    }

    @RequestMapping(value = "/{blogid}", method = RequestMethod.GET)
    public String viewBlogPage(@PathVariable String blogid, ModelMap model) {
        int blogId = Integer.valueOf(blogid);
        Blog blog = blogService.getBlogByBlogId(blogId);
        String userName = userService.getUserNameByUserId(String.valueOf(blog.getBlogAuthor()));
        String tagName = tagService.getTagByTagId(String.valueOf(blog.getBlogTag()));
        model.put("blogEntry", blog);
        model.put("userName", userName);
        model.put("tagName", tagName);
        return "blog/view";
    }

}
